package com.huiquan.icd.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;
import org.springframework.web.servlet.ModelAndView;

import com.huiquan.analysis.domain.User;
import com.huiquan.framework.base.ReturnData;
import com.huiquan.framework.utils.RequestParamUtil;
import com.huiquan.icd.service.IcdDiseaseImageService;

@Controller
public class IcdDiseaseImageController {

	@Autowired
	private IcdDiseaseImageService icdDiseaseImageService;

	/**
	 * status 1-未校验指标；2-报告单；3-检验大类；4-已校验指标；5-失效指标
	 * 
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/icd/disease_image_list.htm")
	public ModelAndView mainList(HttpServletRequest req, HttpServletResponse resp) throws IOException {

		String tabid = req.getParameter("tabid");

		// 维护页面参数到session
		Map<String, Object> pageParam;
		if (tabid != null && !tabid.isEmpty()) {
			pageParam = new HashMap<>();
			pageParam.put("tabid", tabid);
			pageParam.put("start", "1");

			req.getSession().setAttribute("pageParam", pageParam);
		} else {
			pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
		}
		tabid = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "tabid");
		String start = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "start");
		String keyword = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "keyword");
		String countPerPageStr = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "countPerPage");

		return icdDiseaseImageService.getList(Integer.parseInt(start), keyword,countPerPageStr);

	}

	/**
	 * 新增疾病图片
	 *
	 * @param req
	 * @param resp
	 * @param multipartRequest
	 * @return
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping(value = "/icd/insert_disease_image.do")
	public ReturnData insertDiseaseImage(HttpServletRequest req, HttpServletResponse resp,
			MultipartRequest multipartRequest) throws IOException {
		MultipartFile originalImage = multipartRequest.getFile("image1");
		MultipartFile thumbnailImage = multipartRequest.getFile("image2");
		User user = (User) req.getSession().getAttribute("user");
		Map<String, String> params = new HashMap<>();
		params.put("rank", req.getParameter("rank"));
		params.put("disease", req.getParameter("name"));
		return icdDiseaseImageService.insert(params, user, originalImage, thumbnailImage);
	}

	/**
	 * 批量导入疾病图片
	 *
	 * @param req
	 * @param resp
	 * @param multipartRequest
	 * @return
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping(value = "/icd/batch_insert_disease_image.do")
	public ReturnData batchInsertDiseaseImage(HttpServletRequest req, HttpServletResponse resp,
			@RequestParam(required = false) MultipartFile batchFile) throws IOException {
		User user = (User) req.getSession().getAttribute("user");
		return icdDiseaseImageService.insertBatch(user, batchFile);
	}

	/**
	 * 修改疾病图片
	 *
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping(value = "/icd/update_disease_image_name.do")
	public ReturnData updateTestingClass(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
		String disease = req.getParameter("disease");
		String originalName = req.getParameter("originalName");
		String thumbnailName = req.getParameter("thumbnailName");
		String rank = req.getParameter("rank");

		User user = (User) req.getSession().getAttribute("user");

		return icdDiseaseImageService.update(id, disease, originalName, thumbnailName, rank, user);
	}

	/**
	 * 修改疾病图片或者缩图
	 * 
	 * @param req
	 * @param resp
	 * @param file
	 * @return
	 * @throws IOException
	 * @throws IllegalStateException
	 */
	@ResponseBody
	@RequestMapping("/icd/update_disease_image.do")
	public ReturnData updateTestingClassImage(HttpServletRequest req, HttpServletResponse resp,
			@RequestParam(required = false) MultipartFile file) throws IllegalStateException, IOException {
		User user = (User) req.getSession().getAttribute("user");
		String id = req.getParameter("id");
		String type = req.getParameter("type");

		return icdDiseaseImageService.updateImage(id, type, user, file);
	}

	/**
	 * 删除一条记录
	 * 
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/icd/delete_disease_image.do")
	public void deleteTestingClass(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
		icdDiseaseImageService.deleteClass(id);
		resp.sendRedirect("/icd/disease_image_list.htm?tabid=25_13");
	}

}
